/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.DanhMucSanPhamDAO;
import dao.LoaiSanPhamDAO;
import dao.NhaSanXuatDAO;
import dao.SanPhamDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import pojo.Loaisanpham;
import pojo.Nhasanxuat;
import pojo.Sanpham;

/**
 *
 * @author Katori
 */
@WebServlet(name = "ProductController", urlPatterns = {"/ProductController.do"})
public class ProductController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ProductController</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ProductController at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ServletContext application = getServletConfig().getServletContext();
        String action = request.getParameter("action");
        if (action != null) {
            if (action.equals("GetProductTypeList")) {
                int maDanhMuc = Integer.parseInt(request.getParameter("typeID").toString());
                List<Loaisanpham> dsLoaiSP = LoaiSanPhamDAO.layLoaiSanPhamTheoDanhMuc(maDanhMuc);
                if (dsLoaiSP != null) {
                    request.setAttribute("productTypeList", dsLoaiSP);
                    String tenDanhMuc = DanhMucSanPhamDAO.layDanhMucSanPham(maDanhMuc).getTenDanhMuc();
                    request.setAttribute("tenDanhMuc", tenDanhMuc);
                }
                RequestDispatcher rd = request.getRequestDispatcher("ProductType.jsp");
                rd.forward(request, response);
            } else if (action.equals("GetProductList")) {
                int maLoai = Integer.parseInt(request.getParameter("ID").toString());
                List<Sanpham> dsSanPham = SanPhamDAO.laySanPhamTheoLoai(maLoai);
                List<Nhasanxuat> dsNhaSX = NhaSanXuatDAO.layNhaSanXuatTheoLoaiSP(maLoai);
                if (dsSanPham != null && dsNhaSX != null) {
                    request.setAttribute("productList", dsSanPham);
                    request.setAttribute("vendorList", dsNhaSX);

                    String tenLoai = LoaiSanPhamDAO.layLoaiSanPham(maLoai).getTenLoaiSanPham();
                    request.setAttribute("typeName", tenLoai);
                }
                RequestDispatcher rd = request.getRequestDispatcher("ProductList.jsp");
                rd.forward(request, response);
            } else if (action.equals("ProductDetail")) {
                int maSP = Integer.parseInt(request.getParameter("productID").toString());
                Sanpham sp = SanPhamDAO.laySanpham(maSP);
                if (sp != null) {
                    request.setAttribute("product", sp);
                }
                RequestDispatcher rd = request.getRequestDispatcher("ProductDetail.jsp");
                rd.forward(request, response);
            } else if (action.equals("Search")) {
                String term = request.getParameter("searchValue").toString();
                List<Sanpham> sp = SanPhamDAO.laySanPhamTheoTen(term);

                if (sp != null) {
                    request.setAttribute("productList", sp);
                }
                RequestDispatcher rd = request.getRequestDispatcher("Search.jsp");
                rd.forward(request, response);
            }
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
